<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Dbmanager extends CI_Controller {

        // 创建表
        public function createTable()
        {
            // 加载数据库工厂类
            $this->load->dbforge();

            // 设置字段
            $fields = array(
                'id'   =>  array(
                    'type'              =>  'INT',
                    'constraint'        =>  11,
                    'unsigned'          =>  TRUE,
                    'auto_increment'    =>  TRUE
                ),
                'username'    =>  array(
                    'type'              =>  'VARCHAR',
                    'constraint'        =>  20
                ),
                'password'    =>  array(
                    'type'              =>  'CHAR',
                    'constraint'        =>  32
                ),
                'email'    =>  array(
                    'type'              =>  'VARCHAR',
                    'constraint'        =>  50
                )
            );

            // 添加字段
            $this->dbforge->add_field($fields);

            // 添加主键
            $this->dbforge->add_key('id', TRUE);

            // 创建表
            $attributes = array('ENGINE' => 'MyISAM');
            $this->dbforge->create_table('user', TRUE, $attributes);
          }
    }
